package com.relytosoft.mizar.media.analyze.services.controller.system;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.analyze.api.entity.system.AyPlanTemplate;
import com.relytosoft.mizar.media.analyze.api.pojo.dto.AyPlanTemplateDto;
import com.relytosoft.mizar.media.analyze.api.pojo.vo.AyPlanTemplateVo;
import com.relytosoft.mizar.media.analyze.services.service.system.AyPlanTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.simpleframework.xml.core.Validate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author lj
 * @date 2022/5/16 15:12
 */
@RestController
@RequestMapping("analyze/system/plantemplate")
@Api(tags = "分析系统基础接口")
public class AyPlanTemplateController {

    @Resource
    private AyPlanTemplateService ayPlanTemplateService;

    @SaCheckPermission("system:plantemplate:list")
    @OpLog(msg = "模板查询", opType = OpTypeEnum.SELECT)
    @ApiOperation("模板查询")
    @GetMapping("/list")
    public ResponseResult<Map<String, Object>> list(AyPlanTemplateDto ayPlanTemplateDto, PageQuery pageQuery) {
        AyPlanTemplate ayPlanTemplate = BeanCopyUtils.copy(ayPlanTemplateDto, AyPlanTemplate.class);
        IPage<AyPlanTemplate> page = ayPlanTemplateService.selectAyPlanTemplatePageList(pageQuery, ayPlanTemplate);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @SaCheckPermission("system:plantemplate:add")
    @OpLog(msg = "添加模板", opType = OpTypeEnum.INSERT)
    @ApiOperation("添加模板")
    @PostMapping({"/add"})
    public ResponseResult<Void> add(@RequestBody @Validate AyPlanTemplateDto ayPlanTemplateDto) {
        AyPlanTemplate ayPlanTemplate = BeanCopyUtils.copy(ayPlanTemplateDto, AyPlanTemplate.class);
        boolean b = ayPlanTemplateService.addAyPlanTemplate(ayPlanTemplate);
        if (b) {
            return ResponseResult.ok("添加模板成功");
        } else {
            return ResponseResult.fail("添加模板失败");
        }
    }


    @SaCheckPermission("system:plantemplate:id")
    @OpLog(msg = "模板详情", opType = OpTypeEnum.SELECT)
    @ApiOperation("模板详情")
    @GetMapping({"{templateId}"})
    public ResponseResult<AyPlanTemplateVo> id(@PathVariable Integer templateId) {
        return ResponseResult.ok(ayPlanTemplateService.selectAyPlanTemplateById(templateId));
    }

    @SaCheckPermission("system:plantemplate:update")
    @OpLog(msg = "修改模板信息", opType = OpTypeEnum.UPDATE)
    @ApiOperation("修改模板信息")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody AyPlanTemplateDto ayPlanTemplateDto) {
        AyPlanTemplate ayPlanTemplate = BeanCopyUtils.copy(ayPlanTemplateDto, AyPlanTemplate.class);
        boolean b = ayPlanTemplateService.updateAyPlanTemplate(ayPlanTemplate);
        if (b) {
            return ResponseResult.ok("修改模板成功");
        } else {
            return ResponseResult.fail("修改模板失败");
        }
    }

    @SaCheckPermission("system:plantemplate:remove")
    @OpLog(msg = "删除模板信息", opType = OpTypeEnum.DELETE)
    @ApiOperation("删除模板信息")
    @DeleteMapping("del/{templateIds}")
    public ResponseResult<Void> remove(@PathVariable List<Integer> templateIds) {
        boolean b = ayPlanTemplateService.updateAyPlanTemplateStatus(templateIds);
        if (b) {
            return ResponseResult.ok("删除模板成功");
        } else {
            return ResponseResult.fail("删除模板失败");
        }
    }
}
